package com.itheima.mp.domain.po;



import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.List;
//import java.util.List;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.val;

/**
 * <p>
 * 
 * </p>
 *
 * @author author
 * @since 2024-12-03
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("orders_mpapi")
public class OrdersMpapi implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "order_id", type = IdType.ASSIGN_UUID)
    private String orderId;

    private Long customerId;

    private String orderStatus;

    private LocalDateTime orderDate;

    private LocalDateTime deliveryDate;

    public void setOrderDetailItems() {
        this.orderDetailItems = getOrderDetailsAsList();
    }

    private String orderDetails;

    @TableField(exist = false)
    private List<OrderDetailItem> orderDetailItems;

    private Boolean isPaid;

    // 使用 OrderDetailItem 列表设置 orderDetails
    public void setOrderDetailsFromList(List<OrderDetailItem> items)  {
        String jsonPrettyStr = JSONUtil.toJsonPrettyStr(items);
        orderDetails = jsonPrettyStr;
    }

    // 从 orderDetails 字符串获取 OrderDetailItem 列表
    public List<OrderDetailItem> getOrderDetailsAsList()  {
        JSONArray array = JSONUtil.parseArray(orderDetails);

        return JSONUtil.toList(array, OrderDetailItem.class);


    }

    // 直接使用字符串设置 orderDetails
    public void setOrderDetailsFromString(String details) {
        orderDetails = details;
    }

    // 从 orderDetails 字符串获取原始字符串
    public String getOrderDetailsAsString() {
        return orderDetails;
    }


}
